package com.guns.dome1.service.impl;

import com.guns.dome1.dao.IUserDao;
import com.guns.dome1.entity.User;
import com.guns.dome1.service.IUserService;
import com.guns.dome1.session.SessionService;
import com.guns.dome1.utils.PasswordUtil;
import com.guns.dome1.vo.req.ReqUserVo;
import com.guns.dome1.vo.resp.RespBaseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class UserServiceImpl implements IUserService {


    /**
     * dao层通道
     */

    @Autowired
    private IUserDao userDao;
    @Autowired
    private SessionService sessionService;


    //   登录
    @Override
    public RespBaseVo login(ReqUserVo reqUserVo) {
        RespBaseVo respBaseVo = new RespBaseVo();

        String a = reqUserVo.getUsername();

        User daoByName = userDao.findByUsername(a);


        if (daoByName == null) {
            System.out.println("1");
            respBaseVo.setCode(-1);
            respBaseVo.setMsg("用户名不存在或密码错误");
            respBaseVo.setTimestamp(System.currentTimeMillis());
            return respBaseVo;
        }


        System.out.println("reqUserVo");
        System.out.println("getLoginname" + reqUserVo.getUsername());
        System.out.println("getPwd " + reqUserVo.getPwd());

        System.out.println(" ");
        System.out.println("daoByName");
        System.out.println("getLoginname " + daoByName.getUsername());
        System.out.println("getPwd " + daoByName.getPwd());

        System.out.println("测试git pull功能");


        //比较一下输入的密码和数据库中用户名下的密码是否匹配
//        boolean bpaw = daoByName.getPwd().equals(reqUserVo.getPwd());


        String s = PasswordUtil.twiceMd5BySalt(reqUserVo.getPwd());
        System.out.println(s);

        //调用passwordUtil来验证密码
        boolean bpaw = PasswordUtil.checkPassword(daoByName.getPwd(), reqUserVo.getPwd());


        System.out.println(bpaw);

        if (true) {
            String session = sessionService.save();
            String str = null;
            daoByName.setPwd(str);
            respBaseVo.setCode(0);
            respBaseVo.setMsg("登录成功");
            respBaseVo.setTimestamp(System.currentTimeMillis());
            respBaseVo.setData(daoByName);
            respBaseVo.setSession(session);
            return respBaseVo;

        } else {
            System.out.println("2");
            respBaseVo.setCode(-1);
            respBaseVo.setMsg("用户名不存在或密码错误");
            respBaseVo.setTimestamp(System.currentTimeMillis());
            return respBaseVo;
        }


    }

    /**
     * 查询所有
     */

    @Override
    public RespBaseVo list(ReqUserVo reqUserVo) {
        RespBaseVo<Object> respBaseVo = new RespBaseVo<>();

        boolean check = sessionService.check(reqUserVo.getSession());


        if (!check) {
            //session无效或以超时
            System.out.println("用户查询所有时超时");
            respBaseVo.setCode(-2);
            respBaseVo.setMsg("登录已经超时请重新");
            return respBaseVo;
        }


        List<User> userAll = userDao.findAll();

        respBaseVo.setCode(0);
        respBaseVo.setMsg("查询成功");
        respBaseVo.setData(userAll);

        return respBaseVo;
    }


    /**
     * 添加
     */

    @Override
    public RespBaseVo add(ReqUserVo reqUserVo) {
        RespBaseVo<Object> respBaseVo = new RespBaseVo<>();

        boolean check = sessionService.check(reqUserVo.getSession());
        if (!check) {

            //session无效或以超时
            System.out.println("用户添加时超时");

            respBaseVo.setCode(-2);
            respBaseVo.setMsg("登录已经超时请重新");
            return respBaseVo;
        }


        String username = reqUserVo.getUsername();
        String pwd = reqUserVo.getPwd();

        if (username == null || pwd == null) {
            respBaseVo.setCode(-1);
            respBaseVo.setMsg("账户名或密码存在空值");
            return respBaseVo;
        }


        try {
            User user = new User();
            user.setUsername(reqUserVo.getUsername());

            //两次MD5加盐加密
            user.setPwd(PasswordUtil.twiceMd5BySalt(reqUserVo.getPwd()));

            user.setRealName(reqUserVo.getRealName());
            user.setRole(reqUserVo.getRole());
            user.setDeptId(reqUserVo.getDeptId());
            userDao.save(user);

            respBaseVo.setCode(0);
            respBaseVo.setMsg("新增成功");
            return respBaseVo;

        } catch (Exception e) {
            respBaseVo.setCode(-1);
            respBaseVo.setMsg("用户名已存在");
            return respBaseVo;
        }

    }


    /**
     * 修改更新
     */

    @Override
    public RespBaseVo edit(ReqUserVo reqUserVo) {

        RespBaseVo<Object> respBaseVo = new RespBaseVo<>();

        boolean check = sessionService.check(reqUserVo.getSession());
        if (!check) {

            //session无效或以超时
            System.out.println("用户修改时超时");

            respBaseVo.setCode(-2);
            respBaseVo.setMsg("登录已经超时请重新");
            return respBaseVo;
        }

        try {
            //数据库里的user（因为如果不是重制密码，前端提交不过来，所以需要查，如果传过来来null就不进行md5运算）
            User user = userDao.getOne(reqUserVo.getId());

            user.setUsername(reqUserVo.getUsername());
//            user.setPwd(reqUserVo.getPwd());
            //两次MD5加盐加密，不为空才运算

            if (reqUserVo.getPwd() != null) {
                user.setPwd(PasswordUtil.twiceMd5BySalt(reqUserVo.getPwd()));
            }

            user.setRealName(reqUserVo.getRealName());
            user.setRole(reqUserVo.getRole());
            user.setDeptId(reqUserVo.getDeptId());
            userDao.save(user);

            respBaseVo.setCode(0);
            respBaseVo.setMsg("更新成功");
            return respBaseVo;

        } catch (Exception e) {
            respBaseVo.setCode(-1);
            respBaseVo.setMsg("更新失败");
            return respBaseVo;
        }


    }


    /**
     * 删除
     */

    @Override
    public RespBaseVo del(ReqUserVo reqUserVo) {

        RespBaseVo<Object> respBaseVo = new RespBaseVo<>();

        boolean check = sessionService.check(reqUserVo.getSession());
        if (!check) {

            //session无效或以超时
            System.out.println("删除用户超时");

            respBaseVo.setCode(-2);
            respBaseVo.setMsg("删除超时");
            return respBaseVo;
        }

        int id = reqUserVo.getId();

        Optional<User> byId = userDao.findById(id);

        if (byId == null) {
            respBaseVo.setCode(-1);
            respBaseVo.setMsg("用户不存在");
            return respBaseVo;
        }

        try {


            userDao.deleteById(reqUserVo.getId());
            respBaseVo.setCode(0);
            respBaseVo.setMsg("删除成功");
            return respBaseVo;


        } catch (Exception e) {

            respBaseVo.setCode(-1);
            respBaseVo.setMsg("删除失败");
            return respBaseVo;

        }


    }

    //注销
    @Override
    public RespBaseVo logout(ReqUserVo reqUserVo) {
        RespBaseVo<Object> respBaseVo = new RespBaseVo<>();
        sessionService.remove(reqUserVo.getSession());

        respBaseVo.setCode(0);
        respBaseVo.setMsg("注销成功");
        return respBaseVo;
    }


}
